package com.pdstudio.carrecom.ui.activity.buy;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.fourmob.datetimepicker.date.DatePickerDialog;
import com.fourmob.datetimepicker.date.DatePickerDialog.OnDateSetListener;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.bean.Boutique;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.WeatherInfo;
import com.pdstudio.carrecom.bean.WeatherList;
import com.pdstudio.carrecom.logger.Logger;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.tools.UIHelper;
import com.pdstudio.carrecom.ui.activity.owner.ActivityBxxb;
import com.pdstudio.carrecom.ui.adapter.BoutiqueListAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * 类: ActivityExchange <br>
 * 描述: 二手车置换 <br>
 * 作者: ws <br>
 * 时间: 2015-6-16 下午11:15:29
 */
public class ActivityExchange extends FragmentActivity implements OnClickListener, OnDateSetListener {

	private static final String TAG = "ActivityExchange";

	private static final int MSG_WHAT_ABOUT = 1;

	private TextView txtTitle;
	private ImageView ivBack;

	private ResultInfo mResultInfo;

	private TextView mAboutInfo;

	/** 评估按钮 */
	private Button mEval;
	/** 车辆图片 */
	private ImageView mCarImage;
	/** 车辆款型 */
	private EditText mType;
	private EditText mBrand;
	private EditText mSeries;
	
	private LinearLayout mBuyTime;
	
	private DatePickerDialog datePickerDialog;
	private TextView mEndTime;
	
	int type = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_exchange);

		initTitle();
		initView();
		
		final Calendar calendar = Calendar.getInstance();
		datePickerDialog = DatePickerDialog.newInstance(this,
				calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), true);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Logger.i(TAG, msg);
			switch (msg.what) {
			case MSG_WHAT_ABOUT:// 关于
				if (mResultInfo != null) {
					mAboutInfo.setText(mResultInfo.data.toString());
				}
				break;

			default:
				break;
			}
		}
	};

	private Button mNextStep;

	/**
	 * 方法: initTitle <br>
	 * 描述: 初始化页面标题 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午5:46:11
	 */
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("二手车置换");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(this);
	}

	private TextView msBrand;
	private TextView msSerial;
	private TextView msType;
	private TextView msContent;

	private EditText mBuyerMessage;

	private EditText mMil;
	
	private void initView() {
		mEval = (Button) findViewById(R.id.exchange_evaluation);
		mCarImage = (ImageView) findViewById(R.id.exchange_carimg);
		mType = (EditText) findViewById(R.id.exchange_type);
		mEndTime = (TextView) findViewById(R.id.exchange_date);
		mEval.setOnClickListener(this);
		mBuyerMessage = (EditText) findViewById(R.id.more_sug5);
		mBrand = (EditText)findViewById(R.id.exchange_brand);
		mSeries=(EditText)findViewById(R.id.exchange_serial);
		mMil = (EditText)findViewById(R.id.exchange_length);
		
		mNextStep=(Button)findViewById(R.id.exchange_submit);
		mNextStep.setOnClickListener(this);
		
		mBuyTime = (LinearLayout)findViewById(R.id.exchange_buytime);
		mBuyTime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				datePickerDialog.setVibrate(true);
				datePickerDialog.setYearRange(1985, 2028);
				datePickerDialog.setCloseOnSingleTapDay(true);
				datePickerDialog.show(getSupportFragmentManager(), "exchange");
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.exchange_evaluation:
			type=2;
			submit();
			break;
		case R.id.exchange_submit:
			type=3;
			submit();
			break;
		}
	}
	
	private void submit() {
		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mBuyOrderListener);
			RequestParams rps = new RequestParams();
			rps.addBodyParameter("uid", AppContext.getInstance().getUserId()
					+ "");
			rps.addBodyParameter("os", "android");
			rps.addBodyParameter("content",
					StringUtils.utf8Encode(mBuyerMessage.getText().toString()));
			rps.addBodyParameter("model", mType.getText().toString() );
			rps.addBodyParameter("brand", mBrand.getText().toString() );
			rps.addBodyParameter("series", mSeries.getText().toString() );
			rps.addBodyParameter("series", mSeries.getText().toString() );
			rps.addBodyParameter("buyDate", mEndTime.getText().toString());
			rps.addBodyParameter("type", type+"");
			rps.addBodyParameter("mil", mMil.getText().toString());

			http.post(ServiceHelper.ESC, rps);
		} catch (Exception e) {
		}

	}

	/**
	 * 
	 * 方法: getBoutiques <br>
	 * 描述: 获取精品列表 <br>
	 * 作者: ws <br>
	 * 时间: 2015-6-16 上午12:27:40
	 */
	private void getBoutiques() {
		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mGetAbountInfoListener);
			// TODO: 从保存城市地方拿数据
			String url = ServiceHelper.About;
			Logger.i(url);
			http.get(url, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mGetAbountInfoListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			Logger.json(result);
			ResultInfo resultInfo = null;
			try {
				resultInfo = new Gson().fromJson(result,
						new TypeToken<ResultInfo>() {
						}.getType());
				if ((resultInfo != null)
						&& (resultInfo.msg
								.equals(ServiceHelper.msg_code_sucess))) {
					try {
						mResultInfo = resultInfo;
						Logger.i("天气", resultInfo);
						Message msg = new Message();
						msg.obj = mResultInfo;
						msg.what = MSG_WHAT_ABOUT;
						mHandler.sendMessage(msg);

					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int error, String msg) {

		}

		@Override
		public void onCancel() {

		}
	};
	
	private httpReturnJson mBuyOrderListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityExchange.this, "失败！" + msg);
		}

		@Override
		public void onCancel() {
			UIHelper.ToastMessage(ActivityExchange.this, "失败！");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			DoJsonSub(result);
		}
	};
	
	/*
	 * 提交预约
	 */
	private void DoJsonSub(String json) {
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<ResultInfo>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				UIHelper.ToastMessage(ActivityExchange.this, "成功！");
				finish();
			} else {
				UIHelper.ToastMessage(ActivityExchange.this, "失败！"+edv.msg);

			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(ActivityExchange.this, "失败！"+e.getMessage());
		}
	}

	@Override
	public void onDateSet(DatePickerDialog datePickerDialog, int year,
			int month, int day) {
		String endTimeString = year + "-" + (month + 1) + "-" + day;
		mEndTime.setText(endTimeString);
		
	}

}
